package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import com.huike.review.vo.MybatisReviewVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**=========================================================新增数据============================================*/
    /**
     * 功能描述:  新增数据
     * @param name 名字
     * @param age  年龄
     * @param sex   性别
     * @return com.huike.common.core.domain.AjaxResult
     */
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult saveData1(@PathVariable("name") String name,
                                @PathVariable("age") Integer age,
                                @PathVariable("sex") String sex){
        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
        boolean flag = reviewService.saveData1(review);

        if (flag) {
            return AjaxResult.success("成功插入数据");
        }

        return AjaxResult.error("数据插入失败");
    }

    /**
     * 功能描述:  新增数据  Post 请求
     * @param reviewVO
     * @return com.huike.common.core.domain.AjaxResult
     */
    @PostMapping("/saveData")
    public AjaxResult saveData2(@RequestBody MybatisReviewVO reviewVO){

        Review review = new Review();
        review.setName(reviewVO.getName());
        review.setAge(reviewVO.getAge());
        review.setSex(reviewVO.getSex());
        boolean flag = reviewService.saveData1(review);

        if (flag) {
            return AjaxResult.success("成功插入数据");
        }

        return AjaxResult.error("数据插入失败");
    }

    /**=========================================================删除数据=============================================*/
    /**
     * 功能描述:  根据id删除数据
     * @param id
     * @return com.huike.common.core.domain.AjaxResult
     */
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id){

        boolean flag = reviewService.remove(id);

        if (flag) {
            return AjaxResult.success("删除成功");
        }

        return AjaxResult.error("删除失败");
    }

    /**=========================================================修改数据=============================================*/

    /**
     * 功能描述: 根据id修改数据
     * @param reviewVO
     * @return com.huike.common.core.domain.AjaxResult
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody MybatisReviewVO reviewVO){

        Review review = new Review();
        BeanUtils.copyProperties(reviewVO, review);

        boolean flag = reviewService.update(review);

        if (flag) {
            return AjaxResult.success("修改成功");
        }

        return AjaxResult.error("修改失败");
    }

    /**=========================================================查询数据=============================================*/

    /**
     * 功能描述:  单条件查询，根据id来查询数据
     * @param id
     * @return com.huike.common.core.domain.AjaxResult
     */
    @GetMapping("/getById")
    public AjaxResult getById(Long id){

        Review review = reviewService.getById(id);

        MybatisReviewVO reviewVO = new MybatisReviewVO();
        BeanUtils.copyProperties(review, reviewVO);

        return AjaxResult.success(reviewVO);
    }
    // 条件查询
    @GetMapping("/getByCondition")
    public TableDataInfo getById(MybatisReviewVO reviewVO){

        Review review1 = new Review();
        BeanUtils.copyProperties(reviewVO, review1);

        List<Review> reviewList = reviewService.getByCondition(review1);

        List<MybatisReviewVO> reviewVOList = reviewList.stream().map(item -> {

            MybatisReviewVO vo = new MybatisReviewVO();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());

        return getDataTable(reviewVOList);
    }

    /**
     * 功能描述:  分页查询
     * @return com.huike.common.core.page.TableDataInfo
     */
    @GetMapping("/getDataByPage")
    public TableDataInfo getDataByPage(){

        startPage();

        List<Review> reviewList = reviewService.getDataByPage();

        // 数据组装
        List<MybatisReviewVO> reviewVOList = reviewList.stream().map(item -> {

            MybatisReviewVO reviewVO = new MybatisReviewVO();
            BeanUtils.copyProperties(item, reviewVO);

            return reviewVO;
        }).collect(Collectors.toList());

        return getDataTablePage(reviewVOList);
    }




}